<template>
  <div class='wrapper'>
    <div v-for='item in list' :key='item.id' class='cart-item'>
      <van-checkbox v-model='item.checked'></van-checkbox>
      <van-swipe-cell style='width: 100%'>
        <van-card style='width: 100%;' num='1' :price='item.price' :desc='item.desc' :title='item.name'
                  thumb='https://picsum.photos/100/120'>
          <template #tags>
            <van-tag plain type='danger'>8+128</van-tag>
          </template>
          <template #footer>
            <van-button :disabled='!item.count' @click='item.count--' size='small' type='default'>
              <van-icon name='minus' />
            </van-button>
            <van-button size='small'>
              {{ item.count }}
            </van-button>
            <van-button @click='item.count++' size='small' type='default'>
              <van-icon name='plus' />
            </van-button>
          </template>
        </van-card>
        <template #right>
          <van-button @click='cartItemDelete(item.id)' style='height: 100%' type='danger'>删除</van-button>
        </template>
      </van-swipe-cell>
    </div>

    <van-submit-bar tip='左滑删除该商品' :price="4999.00" button-text="立即购买" @submit="onSubmit">
      <van-checkbox v-model="selectAll">全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
export default {
  name: 'Cart',
  data() {
    return {
      list: new Array(6).fill(null).map((_, index) => {
        return {
          id: index,
          name: `商品${index + 1}`,
          desc: '哈哈哈哈' + index,
          checked: false,
          price: '123.' + index,
          count: 1
        }
      })
    }
  },
  computed: {
    selectAll: {
      get() {
        return this.list.every(item => item.checked)
      },
      set(val) {
        this.list.forEach(item => item.checked = val)
      }
    }
  },
  methods: {
    cartItemDelete(id){
      this.list = this.list.filter(item => item.id != id)
      this.$toast.success("删除成功")
    },
    onSubmit(){
      const data = this.list.filter(item => item.checked)
      this.$router.push({
        name: 'createOrder',
        params: data
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.wrapper {
  display: flex;
  flex-direction: column;
  margin-bottom: 34px;
  padding: 10px;
  background: #fff;
  gap: 5px;
}

.van-goods-action-button {
  padding: 0 20px;
  flex: none;
  width: max-content;
}

.cart-item {
  display: flex;
  background: #F8F8F8;
  padding: 5px;
  position: relative;
  gap: 5px;

  img {
    width: 100px;
    height: 120px;
  }

  .van-checkbox {
    position: absolute;
    top: 15px;
    left: 15px;
    z-index: 10;
  }
}

.price {
  color: chocolate;
  font-size: 16px;
}
</style>
